<template>
	<view v-if="is_jinyan||live_status">
		<view class="title_box">
			<view class="block"></view>
			<view>留言板</view>
		</view>
		<view class="box-1" id="list-box" v-if="Height">
			<scroll-view :style="'height:'+Height+'px;'" :scroll-top="scrollTop" :scroll-into-view="scrollIntoView"
				class="talk-list" scroll-y="true" v-if="talkList.length>0 && is_jinyan==2">
				<view v-for="(item,index) in talkList" :key="index" :id="`msg-${item.id}`">
					<view class="time">{{item.msg_time}}</view>
					<view class="item flex_col name" :class=" item.user_id == userInfo.id ? 'push':'pull' ">
						{{item.nickname}}
					</view>
					<view class="item flex_col" :class=" item.user_id == userInfo.id ? 'push':'pull' ">
						<image src="@/static/images/default_avatar.png" mode="aspectFill" class="pic"></image>
						<view class="content">{{item.content}}</view>
					</view>
				</view>
			</scroll-view>
			<view class="talk-list noData" v-if="talkList.length==0 && is_jinyan==2">
				<image src="../../static/images/nodata.png" class="image"></image>
				<view class="text">暂时还没有人发言～</view>
			</view>
			<view class="talk-list forbiddenSpeech" v-if="is_jinyan==1">
				<image src="../../static/images/forbiddenSpeech.png" class="image"></image>
				<view class="text">当前留言栏处于禁言中，无法发送消息!</view>
			</view>
		</view>
		<view class="box-2">
			<view class="flex_col">
				<view class="flex_grow">
					<input type="text" class="content" v-model="content" :disabled="is_jinyan ==1"
						:placeholder="is_jinyan ==1?'禁言中,无法发送消息':'请输入聊天内容'" placeholder-style="color:#DDD;"
						:cursor-spacing="6">
				</view>
				<button class="send" @tap="send">发送</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex";
	import {
		messageList,
		liveroomMessage,
	} from "@/api/api.js"
	export default {
		props: {
			Height: {
				type: Number,
				default: 300,
			},
			Id: {
				type: Number,
				default: null,
			},
			is_jinyan: {
				type: Number,
				default: 1 //是否禁言
			},
			live_status: {
				type: Number,
				default: 1 //直播状态
			},
		},
		computed: {
			...mapGetters(['userInfo'])
		},
		watch: {
			is_jinyan: {
				handler(val, oldVal) {
					let that = this
					if (val == 2 && this.live_status == 2) {
						this.liveInterval = setInterval(async () => {
							that.talkList = await that.getHistoryMsg()
						}, 1000)
					} else {
						clearInterval(this.liveInterval)
					}
				},
				immediate: true
			}
		},
		data() {
			return {
				talkList: [],
				content: '',
				scrollTop: 0,
				scrollIntoView: null,
				liveInterval: null
			}
		},
		async mounted() {
			let that = this
			this.talkList = await this.getHistoryMsg()
			if (this.talkList.length > 0) {
				this.scrollTop = this.talkList.length * 100
			}
		},
		destroyed() {
			clearInterval(this.liveInterval)
		},
		methods: {
			// 获取历史消息
			async getHistoryMsg() {
				let {
					data
				} = await messageList({
					live_id: this.Id
				});
				return data
			},
			// 发送信息
			send() {
				if (this.is_jinyan == 1) {
					this.content = ""
					return this.$Tips({
						title: "直播禁言中，无法发送消息!"
					})
				}
				if (this.live_status == 3) {
					this.content = ""
					return this.$Tips({
						title: "直播已结束，无法发送消息!"
					})
				}
				if (!this.content) {
					return;
					uni.showToast({
						title: '请输入有效的内容',
						icon: 'none'
					})
				}
				uni.showLoading({
					title: '正在发送'
				})
				liveroomMessage({
					live_id: this.Id,
					content: this.content,
				}).then(async (res) => {
					uni.hideLoading();
					this.content = ""
					this.talkList = await this.getHistoryMsg()
					if (this.talkList.length > 0) {
						this.scrollTop = this.talkList.length * 100
					}
					// console.log(this.talkList)
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "./chat.scss";

	page {
		background-color: #F3F3F3;
		font-size: 28rpx;
	}

	.noData {
		padding-top: 40rpx;
		margin: 0 auto;
		text-align: center;

		.image {
			width: 320rpx;
			height: 320rpx;
		}

		.text {
			font-size: 28rpx;
			color: #B0B0B0;
		}
	}

	.forbiddenSpeech {
		padding-top: 200rpx;
		margin: 0 auto;
		text-align: center;

		.image {
			width: 48rpx;
			height: 48rpx;
		}

		.text {
			margin-top: 20rpx;
			font-size: 28rpx;
			color: #B0B0B0;
		}
	}

	/* 加载数据提示 */
	.tips {
		position: fixed;
		left: 0;
		top: var(--window-top);
		width: 100%;
		z-index: 9;
		background-color: rgba(0, 0, 0, 0.15);
		height: 72rpx;
		line-height: 72rpx;
		transform: translateY(-80rpx);
		transition: transform 0.3s ease-in-out 0s;

		&.show {
			transform: translateY(0);
		}
	}

	.title_box {
		position: fixed;
		padding: 0 20rpx;
		box-sizing: border-box;
		width: 100%;
		z-index: 400;
		height: 84rpx;
		background-color: #ffffff;
		font-size: 30rpx;
		color: #000000;
		display: flex;
		align-items: center;
		border-left: 10rpx solid #49A1FD;

		.block {
			width: 10rpx;
			height: 30rpx;
			border-radius: 10rpx;
			margin-right: 10rpx;
		}
	}

	.box-1 {
		width: 100%;
		height: auto;
		padding-top: 84rpx;
		padding-bottom: 108rpx;
		box-sizing: content-box;

		/* 兼容iPhoneX */
		margin-bottom: 0;
		margin-bottom: constant(safe-area-inset-bottom);
		margin-bottom: env(safe-area-inset-bottom);
	}

	.box-2 {
		position: fixed;
		left: 0;
		width: 100%;
		bottom: 0;
		height: auto;
		z-index: 2;
		border-top: #e5e5e5 solid 1px;
		box-sizing: content-box;
		background-color: #F6F6F6;

		/* 兼容iPhoneX */
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		>view {
			padding: 0 20rpx;
			height: 108rpx;
		}

		.content {
			background-color: #fff;
			height: 76rpx;
			padding: 0 20rpx;
			border-radius: 12rpx;
			font-size: 28rpx;
		}

		.send {
			background-color: #49A1FD;
			color: #fff;
			height: 76rpx;
			margin-left: 20rpx;
			border-radius: 12rpx;
			padding: 0;
			width: 152rpx;
			line-height: 76rpx;

			&:active {
				background-color: #49A1FD;
			}
		}
	}

	.talk-list {
		// padding-bottom: 20rpx;

		.time {
			font-size: 24rpx;
			color: #BDBDBD;
			text-align: center;
			padding: 20rpx 0 0;
		}

		.name {
			margin: 10rpx 20rpx;
			font-size: 28rpx;
			color: #000000
		}

		/* 消息项，基础类 */
		.item {
			padding: 0rpx 20rpx 0 20rpx;
			align-items: flex-start;
			align-content: flex-start;
			color: #333;

			.pic {
				width: 92rpx;
				height: 92rpx;
				border-radius: 50%;
				border: #fff solid 1px;
			}

			.content {
				padding: 20rpx;
				border-radius: 4px;
				max-width: 500rpx;
				word-break: break-all;
				line-height: 52rpx;
				position: relative;
			}

			/* 收到的消息 */
			&.pull {
				.content {
					margin-left: 32rpx;
					background-color: #fff;

					&::after {
						content: '';
						display: block;
						width: 0;
						height: 0;
						border-top: 16rpx solid transparent;
						border-bottom: 16rpx solid transparent;
						border-right: 20rpx solid #fff;
						position: absolute;
						top: 30rpx;
						left: -18rpx;
					}
				}
			}

			/* 发出的消息 */
			&.push {
				/* 主轴为水平方向，起点在右端。使不修改DOM结构，也能改变元素排列顺序 */
				flex-direction: row-reverse;

				.content {
					margin-right: 32rpx;
					background-color: #49A1FD;
					color: #ffffff;

					&::after {
						content: '';
						display: block;
						width: 0;
						height: 0;
						border-top: 16rpx solid transparent;
						border-bottom: 16rpx solid transparent;
						border-left: 20rpx solid #49A1FD;
						position: absolute;
						top: 30rpx;
						right: -18rpx;
					}
				}
			}
		}
	}
</style>